package com.technology.education.ui.student.teacher

import android.widget.Button
import android.widget.ImageView
import com.angcyo.dsladapter.toDslItemList
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.SpKeys
import com.technology.education.data.model.TeacherInfoModel
import com.technology.education.ext.header
import com.technology.education.ext.notifyDialog
import com.technology.education.ext.postEventBus
import com.technology.education.ext.setPageItems
import me.hgj.jetpackmvvm.ext.parseState

/**
 *@author:create by BZY
 *Date: 2022/3/21 10
 */
class TeacherListFragment : BaseRecyclerTopBarFragment<TeacherListViewModel>() {
    override fun initViews() {
        setTitle("教师列表")
    }

    override fun lazyLoadData() {
        loading()
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { data ->
                hasMore(data.total)
                renderAdapter {
                    setPageItems(data.list.toDslItemList(R.layout.item_follow_teacher) {
                        itemBindOverride =
                            { itemHolder, _, _, _ ->
                                itemData?.let { it ->
                                    if (it is TeacherInfoModel) {
                                        itemHolder.tv(R.id.tv_name)?.text = it.name
                                        itemHolder.v<ImageView>(R.id.iv_header)?.apply {
                                            header(it.avatar, this)
                                        }
                                        itemHolder.v<Button>(R.id.btn_call)?.text="选择"
                                        itemHolder.visible(R.id.tv_collect, it.isCollected)
                                        itemHolder.click(R.id.btn_call){_->
                                            notifyDialog(notify = "是否指定${it.name}老师为您进行辅导?"){
                                                postEventBus(SpKeys.SELECT_TEACHER,it)
                                                onBack()
                                            }
                                        }
                                    }
                                }
                            }
                    }, mViewModel.pageIndex)
                }
            })
        }
    }
}